package com.duowei.diandan.adapter;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.Taste;
import com.duowei.diandan.databinding.TasteRequiredItemBinding;
import com.duowei.diandan.db.entry.Dmpzsd;
import com.duowei.diandan.presenter.TasteRequiredItemPresenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 必选口味Adapter
 */
public class TasteRequiredAdapter extends RecyclerView.Adapter<TasteRequiredAdapter.ViewHolder> {
    private List<Taste> mList;

    public TasteRequiredAdapter(List<Taste> list) {
        mList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        TasteRequiredItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),
                R.layout.recycler_item_taste_required, parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(mList.get(holder.getAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public List<Taste> getList() {
        return mList;
    }

    public List<Dmpzsd> getCheckedList() {
        List<Dmpzsd> list = new ArrayList<>();

        for (Taste t : mList) {
            if (t.isChecked()) {
                list.add(t.getDmpzsd());
            }
        }

        return list;
    }

    public String getCheckedString() {
        List<Dmpzsd> dmpzsdList = getCheckedList();
        if (dmpzsdList.size() == 0) {
            return null;
        } else {
            StringBuilder stringBuilder = new StringBuilder();
            for (Dmpzsd d : dmpzsdList) {
                stringBuilder.append(String.format(Locale.CHINA, "(%s)", d.getNr()));
            }

            return stringBuilder.toString();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TasteRequiredItemBinding mBinding;

        public ViewHolder(TasteRequiredItemBinding binding) {
            super(binding.getRoot());
            mBinding = binding;
        }

        public void bind(Taste item) {
            mBinding.setItem(item);
            mBinding.setPresenter(new TasteRequiredItemPresenter());
            mBinding.executePendingBindings();
        }
    }
}
